package com.moyu.user.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.alibaba.nacos.api.model.v2.Result;
import com.moyu.common.core.domain.R;
import com.moyu.user.domain.permission.Permission;
import com.moyu.user.domain.role.Role;
import com.moyu.user.service.IRoleService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 赵彦博
 * @Date 2024/11/19 17:38
 */
@RestController
@RequestMapping("roles")
@Tag(name = "角色接口")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @SaCheckRole("角色管理员")
    @PostMapping("createRole")
    public R<Role> createRole(@RequestBody Role role) {
        return R.ok(roleService.createRole(role));
    }

    @SaCheckRole("角色管理员")
    @PutMapping("updateRole")
    public R<Role> updateRole(@RequestBody Role role) {
        return R.ok(roleService.update(role));
    }

    @SaCheckRole("角色管理员")
    @DeleteMapping("{id}")
    public R deleteRole(@PathVariable Long id) {
        roleService.delete(id);
        return R.ok();
    }

    @GetMapping("{username}")
    public R<Role> queryUser(@PathVariable String roleName) {
        Role role = roleService.queryByRoleName(roleName);
        return R.ok(role);
    }

    @GetMapping("list")
    public R<List<Role>> listRole() {
        return R.ok(roleService.list());
    }

    @GetMapping("{id}/permissions")
    public R<List<Permission>> listPermission(@PathVariable Long id, @RequestParam(defaultValue = "false") boolean treeMode) {
        return R.ok(roleService.listPermission(id, treeMode));
    }

    @SaCheckRole("角色管理员")
    @PostMapping("{id}/permissions")
    public R createPermission(@PathVariable Long id, @RequestBody List<Permission> permissions) {
        roleService.createPermission(id, permissions);
        return R.ok();
    }

    @SaCheckRole("角色管理员")
    @PutMapping("{id}/permissions")
    public R updatePermission(@PathVariable Long id, @RequestBody List<Permission> permissions) {
        roleService.updatePermission(id, permissions);
        return R.ok();
    }

}
